<?php
class YWidget extends CWidget{
	public $id = null;		//WidgetID
	public $tpl = 'normal';	//模板
	public $class = null; 	//样式
	
	/**
	 * CClientScript
	 * @var CClientScript
	 */
	protected $cs = null;			//CClientScript
	protected $class_name = null;	//当前类名
	protected $full_class = null;	//当前Widget的样式
	
	public function init(){
		$this->cs = Yii::app()->clientScript;
		$this->class_name = get_class($this);

		if(empty($this->id)){
			$this->id = $this->class_name . '_' . rand(10000, 99999);
		}
		
		$widget_class = preg_replace("/([A-Z])/e", "strtolower('_\\0')", $this->class_name);
		$widget_class = substr($widget_class, 1);
		$widget_class .= ' ' . $widget_class . '_' . $this->tpl;
		
		if($this->class != null){
			$this->full_class = $this->class . ' ' . $widget_class;
		}else{
			$this->full_class = $widget_class;
		}
	}
	
	/**
	 * 尝试加载本widget的资源
	 * @param unknown $type
	 */
	protected function load_resource($name = null){
		if($name === null){
			$name = $this->tpl;
		}
		$file_name = '/widget/' . $this->class_name . '/' . $name;
		if(file_exists(ROOT_PATH . $file_name . '.css')){
			$this->cs->registerCssFile($file_name . '.css');
		}
		if(file_exists(ROOT_PATH . $file_name . '.js')){
			$this->cs->registerScriptFile($file_name . '.js', CClientScript::POS_END);
		}
	}
}

?>